<template>
  <div :style="{ ...coreStyle.marginSame(8) }">
    <a-card>
      <a-form ref="staffFormQueryRef" :model="staffFormQueryModel" labelAlign="left" layout="vertical" :style="{ ...coreStyle.marginBottom(10) }">
        <a-row :gutter="16" v-show="!staffFormQueryVisible">
          <a-col :span="8">
            <a-form-item :style="{ ...coreStyle.marginBottom(-8) }">
              <a-input-search v-model:value="staffFormQueryModel.keySearchVo" placeholder="关键字搜索" :disabled="staffFormQuerySubmitting" @search="staffFormQuerySearch" />
            </a-form-item>
          </a-col>
          <a-col :span="16">
            <a-button type="link" :style="{ ...coreStyle.floatRight }" @click="staffFormQueryUpDown">
              展开
              <CoreIcon icon="akar-icons:chevron-down" :size="14" />
            </a-button>
          </a-col>
        </a-row>
        <a-row :gutter="16" v-show="staffFormQueryVisible">
          <a-col :span="8">
            <a-form-item>
              <a-input-search v-model:value="staffFormQueryModel.keySearchVo" placeholder="关键字搜索" :disabled="staffFormQuerySubmitting" @search="staffFormQuerySearch" />
            </a-form-item>
          </a-col>
          <a-col :span="16">
          </a-col>
          <a-col :span="8">
            <a-form-item label="工号">
              <a-input v-model:value="staffFormQueryModel.staffNoAndLike" placeholder="输入工号" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="姓名">
              <a-input v-model:value="staffFormQueryModel.staffNameAndLike" placeholder="输入姓名" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <!-- <a-form-item label="性别">
              <a-input v-model:value="staffFormQueryModel.genderAndLike" placeholder="输入性别" />
            </a-form-item> -->
            <Dict v-model="staffFormModel.gender" dictKey="staffGender" formItemName="gender" />
          </a-col>
          <a-col :span="8">
            <a-form-item label="紧急联系人姓名">
              <a-input v-model:value="staffFormQueryModel.emergContNameAndLike" placeholder="输入紧急联系人姓名" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="手机号">
              <a-input v-model:value="staffFormQueryModel.mphoneNoAndLike" placeholder="输入手机号" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="身份证号">
              <a-input v-model:value="staffFormQueryModel.idCardAndLike" placeholder="输入身份证号" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="紧急联系人手机号">
              <a-input v-model:value="staffFormQueryModel.emergContMphoneNoAndLike" placeholder="输入紧急联系人手机号" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="邮箱">
              <a-input v-model:value="staffFormQueryModel.emailAndLike" placeholder="输入邮箱" />
            </a-form-item>
          </a-col>
          <!-- <a-col :span="8">
            <a-form-item label="描述">
              <a-input v-model:value="staffFormQueryModel.descriptionAndLike" placeholder="输入描述" />
            </a-form-item>
          </a-col> -->
          <!-- <a-col :span="8">
            <a-form-item label="排序权重">
              <a-input-number v-model:value="staffFormQueryModel.weightOrder" :style="{ ...coreStyle.width('100%') }" />
            </a-form-item>
          </a-col> -->
          <a-col :span="24">
            <a-space :style="{ ...coreStyle.floatRight }">
              <a-button @click="staffFormQueryReset">
                <CoreIcon icon="ant-design:clear-outlined" :size="14" />
                重置
              </a-button>
              <a-button type="primary" ghost :loading="staffFormQuerySubmitting" @click="staffFormQuerySearch">
                <CoreIcon icon="ant-design:search-outlined" :size="14" />
                查询
              </a-button>
              <a-button type="link" @click="staffFormQueryUpDown">
                收起
                <CoreIcon icon="akar-icons:chevron-up" :size="14" />
              </a-button>
            </a-space>
          </a-col>
        </a-row>
      </a-form>
      <a-space>
        <a-button type="primary" ghost @click="staffAdd">
          <CoreIcon icon="carbon:add-alt" :size="14" />
          新增
        </a-button>
        <a-button type="dashed" @click="staffUpdate" v-if="staffListSelectLength === 1">
          <CoreIcon icon="ri:brush-line" :size="14" />
          修改
        </a-button>
        <a-popconfirm title="确认删除？" placement="bottom" @confirm="staffDelete" v-if="staffListSelectLength > 0">
          <a-button type="dashed" danger>
            <CoreIcon icon="jam:delete" :size="14" />
            删除
          </a-button>
        </a-popconfirm>
        <a-button @click="staffDetailInfo" v-if="staffListSelectLength === 1">
          <CoreIcon icon="heroicons-outline:light-bulb" :size="14" />
          详情
        </a-button>
      </a-space>
      <a-space :style="{ ...coreStyle.floatRight }">
        <a-button type="link" shape="circle" @click="staffListGetDataSet">
          <CoreIcon icon="ci:refresh" :size="22" />
        </a-button>
      </a-space>
      <a-table
        rowKey="id"
        :columns="staffListColumns"
        :data-source="staffListDataSet"
        :loading="staffListLoading"
        :pagination="false"
        bordered
        :customRow="staffListCustomRow"
        :row-selection="{ fixed: true, selectedRowKeys: staffListSelectedRowKeys, onChange: staffListRowOnChange }"
        :scroll="{ x: '100%', y: 550 }"
        :style="{ ...coreStyle.marginTop(10) }"
      >
        <template #genderData="{ text }">
          <span>
            <a-tag color="blue" :style="{ 'line-height': '19px' }" v-if="'1' === text">{{ coreToolStore.getDictDisplayName('staffGender', text) }}</a-tag>
            <a-tag color="pink" :style="{ 'line-height': '19px' }" v-if="'2' === text">{{ coreToolStore.getDictDisplayName('staffGender', text) }}</a-tag>
            <a-tag color="default" :style="{ 'line-height': '19px' }" v-if="'0' === text">{{ coreToolStore.getDictDisplayName('staffGender', text) }}</a-tag>
          </span>
        </template>
      </a-table>
      <a-pagination
        v-model:current="staffListDataPage"
        v-model:pageSize="staffListDataRows"
        :total="staffListDataCount"
        show-size-changer
        show-quick-jumper
        :page-size-options="[ ...coreStyle.antdPaginationPageSizeOptions ]"
        :show-total="(total, range) => coreStyle.antdPaginationShowTotal(total, range)"
        :style="{ ...coreStyle.marginTop(10), ...coreStyle.textAlignCenter }"
        @change="staffListPaginationChange"
        @showSizeChange="staffListPaginationShowSizeChange"
        v-if="staffListPaginationVisible"
      >
      </a-pagination>
      <p :style="{ ...coreStyle.marginTop(14), ...coreStyle.marginBottom(6), ...coreStyle.textAlignCenter }" v-if="!staffListPaginationVisible">
        {{ coreStyle.antdPaginationShowTotal(staffListDataCount) }}
      </p>
    </a-card>
    <a-drawer
      v-model:visible="staffEditDrawerVisible"
      title="编辑员工信息"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
      @close="staffEditDrawerOnClose"
    >
      <a-form ref="staffFormRef" :model="staffFormModel" :rules="staffFormRules" labelAlign="left" layout="vertical">
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item label="工号" name="staffNo">
              <a-input v-model:value="staffFormModel.staffNo" placeholder="输入工号" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="手机号" name="mphoneNo">
              <a-input v-model:value="staffFormModel.mphoneNo" placeholder="输入手机号" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="姓名" name="staffName">
              <a-input v-model:value="staffFormModel.staffName" placeholder="输入姓名" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="身份证号" name="idCard">
              <a-input v-model:value="staffFormModel.idCard" placeholder="输入身份证号" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <!-- <a-form-item label="性别" name="gender">
              <a-input v-model:value="staffFormModel.gender" placeholder="输入性别" />
            </a-form-item> -->
            <Dict v-model="staffFormModel.gender" dictKey="staffGender" formItemName="gender" />
          </a-col>
          <a-col :span="12">
            <a-form-item label="排序权重" name="weightOrder">
              <a-input-number v-model:value="staffFormModel.weightOrder" :style="{ ...coreStyle.width(200) }" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="紧急联系人姓名" name="emergContName">
              <a-input v-model:value="staffFormModel.emergContName" placeholder="输入紧急联系人姓名" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="邮箱" name="email">
              <a-input v-model:value="staffFormModel.email" placeholder="输入邮箱" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="紧急联系人手机号" name="emergContMphoneNo">
              <a-input v-model:value="staffFormModel.emergContMphoneNo" placeholder="输入紧急联系人手机号" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
          </a-col>
          <FileUpload ref="staffFormFileUploadRef" fileModule="staffAvatar" @change="staffFormFileUploadChange" />
          <a-col :span="24">
            <a-form-item label="头像url" name="avatarUrl">
              <!-- <a-input v-model:value="staffFormModel.avatarUrl" placeholder="输入头像url" /> -->
              <a-input v-model:value="staffFormModel.avatarUrl" disabled placeholder="输入头像url">
                <template #addonAfter>
                  <a-button type="link" size="small" @click="staffFormAvatarUrlSelectFileClick">选择文件</a-button>
                </template>
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="岗位列表" name="postList">
              <a-tree-select v-model:value="staffFormModel.postList" :tree-data="staffPostListOrgPostTreeData" treeNodeFilterProp="title" multiple showSearch allowClear treeDefaultExpandAll placeholder="请选择" :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }" @change="staffPostListChange">
              </a-tree-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="默认岗位" name="defaultPostId">
              <a-tree-select v-model:value="staffFormModel.defaultPostId" :tree-data="staffDefaultPostIdOrgPostTreeData" treeNodeFilterProp="title" showSearch allowClear treeDefaultExpandAll placeholder="请选择" :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }">
              </a-tree-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="描述" name="description">
              <a-textarea v-model:value="staffFormModel.description" :rows="3" placeholder="输入描述" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button @click="staffFormReset">
            <CoreIcon icon="ant-design:clear-outlined" :size="14" />
            重置
          </a-button>
          <a-button type="primary" ghost :loading="staffFormSaveSubmitting" @click="staffFormNextAdd">
            <CoreIcon icon="tabler:player-track-next" :size="14" />
            继续录入
          </a-button>
          <a-button type="primary" ghost :loading="staffFormSaveSubmitting" @click="staffFormSave">
            <CoreIcon icon="ant-design:save-outlined" :size="14" />
            保存
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="staffDetailDrawerVisible"
      title="员工详细信息"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
    >
      <a-space direction="vertical" :size="20" :style="{ ...coreStyle.width('100%') }">
        <a-descriptions title="基础信息" :column="2" bordered>
          <a-descriptions-item label="工号" :span="1">{{ staffDetailModel.staffNo }}</a-descriptions-item>
          <a-descriptions-item label="排序权重" :span="1">{{ staffDetailModel.weightOrder }}</a-descriptions-item>
          <a-descriptions-item label="姓名" :span="1">{{ staffDetailModel.staffName }}</a-descriptions-item>
          <a-descriptions-item label="性别" :span="1">{{ coreToolStore.getDictDisplayName('staffGender', staffDetailModel.gender) }}</a-descriptions-item>
          <a-descriptions-item label="手机号" :span="1">{{ staffDetailModel.mphoneNo }}</a-descriptions-item>
          <a-descriptions-item label="身份证号" :span="1">{{ staffDetailModel.idCard }}</a-descriptions-item>
          <a-descriptions-item label="紧急联系人姓名" :span="1">{{ staffDetailModel.emergContName }}</a-descriptions-item>
          <a-descriptions-item label="紧急联系人手机号" :span="1">{{ staffDetailModel.emergContMphoneNo }}</a-descriptions-item>
          <a-descriptions-item label="邮箱" :span="2">{{ staffDetailModel.email }}</a-descriptions-item>
          <a-descriptions-item label="头像" :span="2">{{ staffDetailModel.avatarUrl }}</a-descriptions-item>
          <a-descriptions-item label="描述" :span="2">{{ staffDetailModel.description }}</a-descriptions-item>
        </a-descriptions>
      </a-space>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button type="dashed" @click="staffDetailUpdate">
            <CoreIcon icon="ri:brush-line" :size="14" />
            修改
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="staffEditFileSelectDrawerVisible"
      title="文件选择"
      placement="right"
      :closable="false"
      destroyOnClose
      :width="'80%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 0, 0) }"
    >
      <FileSelect fileModule="staffAvatar" @change="staffFormFileSelectChange" />
    </a-drawer>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from 'vue-class-component'
import { mapStores } from 'pinia'
import { useCoreToolStore } from '/@/store/modules/cn/core/tool/coreToolStore'
import type { Res } from '/@/tool/Res'
import type { Param } from '/@/tool/Param'
import type { CoreTree } from '/@/tool/CoreTree'

import { useStaffStore } from '/@/store/modules/cn/core/sign/staffStore'

import type { Staff } from '/@/model/cn/core/sign/eo/Staff'
import { toStaff } from '/@/model/cn/core/sign/eo/Staff'

import type { StaffQO } from '/@/model/cn/core/sign/qo/StaffQO'
import { toStaffQO } from '/@/model/cn/core/sign/qo/StaffQO'

import type { StaffVO } from '/@/model/cn/core/sign/vo/StaffVO'
import { toStaffVO } from '/@/model/cn/core/sign/vo/StaffVO'

import { usePostStore } from '/@/store/modules/cn/core/sign/postStore'

import type { PostQO } from '/@/model/cn/core/sign/qo/PostQO'

@Options({
  name: 'CoreStaff',
  components: {
  },
  computed: {
    ...mapStores(useCoreToolStore, useStaffStore, usePostStore),
  },
})
export default class CoreStaff extends Vue {

  staffListDataSet: Array<StaffVO> = []

  staffListColumns: Array<object> = [
    {
      title: '工号',
      dataIndex: 'staffNo',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: '姓名',
      dataIndex: 'staffName',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: '手机号',
      dataIndex: 'mphoneNo',
      align: 'center',
      // ellipsis: true,
      width: 200,
    },
    // {
    //   title: '身份证号',
    //   dataIndex: 'idCard',
    //   align: 'center',
    //   // ellipsis: true,
    //   width: 300,
    // },
    // {
    //   title: '紧急联系人姓名',
    //   dataIndex: 'emergContName',
    //   align: 'center',
    //   // ellipsis: true,
    //   // width: 200,
    // },
    // {
    //   title: '紧急联系人手机号',
    //   dataIndex: 'emergContMphoneNo',
    //   align: 'center',
    //   // ellipsis: true,
    //   // width: 200,
    // },
    // {
    //   title: '描述',
    //   dataIndex: 'description',
    //   align: 'center',
    //   // ellipsis: true,
    //   // width: 200,
    // },
    {
      title: '性别',
      dataIndex: 'gender',
      align: 'center',
      slots: { customRender: 'genderData' },
      // ellipsis: true,
      width: 100,
    },
    {
      title: '排序权重',
      dataIndex: 'weightOrder',
      align: 'center',
      // ellipsis: true,
      width: 100,
    },
  ]

  staffListLoading: boolean = false

  staffListSelectedRowKeys: string[] = []

  staffListSelectData: Array<StaffVO> = []

  staffListSelectLength: number = 0

  staffListSelectCurrentRowIndex: number = -1

  staffListSelectCurrentRowData: StaffVO = toStaffVO()

  staffListPaginationVisible: boolean = true

  staffListDataPage: number = 1

  staffListDataRows: number = 10

  staffListDataCount: number = 0

  staffListRowOnChange(selectedRowKeys, selectedRows) {
    this.staffListSelectedRowKeys = [ ...selectedRowKeys ]
    this.staffListSelectData = [ ...selectedRows ]
    this.staffListSelectLength = selectedRows.length
  }

  staffListCustomRow(record, index) {
    return {
      'class': index === this.staffListSelectCurrentRowIndex ? 'ant-table-row-selected' : '',
      onClick: (event) => {
        this.staffListSelectCurrentRowIndex = index
        this.staffListSelectCurrentRowData = { ...record }
        let selectedRowKeys = [ ...this.staffListSelectedRowKeys ]
        let selectedRows = [ ...this.staffListSelectData ]
        if (selectedRowKeys.includes(record.id)) {
          this.staffListRowOnChange([ ...selectedRowKeys.filter(eo => { return eo !== record.id }) ], [ ...selectedRows.filter(eo => { return eo.id !== record.id }) ])
        } else {
          selectedRowKeys.push(record.id)
          selectedRows.push(record)
          this.staffListRowOnChange([ ...new Set(selectedRowKeys) ], [ ...new Set(selectedRows) ])
        }
      },
      onDblclick: (event) => {
        this.staffStore.pkVo(this.$toParam<StaffQO>({ id: this.staffListSelectCurrentRowData.id } as StaffQO)).then((res: Res<StaffVO>) => {
          if (res.succ) {
            this.staffDetailModel = { ...res.data[0] }
            this.staffDetailDrawerVisible = true
          } else {
            this.$message.error(res.msg, 6)
          }
        })
      },
    }
  }

  staffListGetDataSet() {
    this.staffListLoading = true
    this.staffListRowOnChange([], [])
    this.staffListSelectCurrentRowIndex = -1
    this.staffListSelectCurrentRowData = toStaffVO()
    this.staffStore.pagingVo(this.$toParam<StaffQO>([], this.staffListDataPage, this.staffListDataRows)).then((res: Res<StaffVO>) => {
      if (res.succ) {
        this.staffListPaginationVisible = true
        this.staffListDataSet = [ ...res.data ]
        this.staffListDataCount = res.count
        setTimeout(() => { this.staffListLoading = false }, 500)
      } else {
        this.$message.error(res.msg, 6)
        setTimeout(() => { this.staffListLoading = false }, 500)
      }
    }).catch(err => {
      setTimeout(() => { this.staffListLoading = false }, 500)
    })
  }

  staffListPaginationChange() {
    this.staffListGetDataSet()
  }

  staffListPaginationShowSizeChange() {
    this.staffListGetDataSet()
  }

  staffFormQueryVisible: boolean = false

  staffFormQuerySubmitting: boolean = false

  staffFormQueryModel: StaffQO = toStaffQO()

  staffFormQueryUpDown() {
    this.staffFormQueryVisible = !this.staffFormQueryVisible
  }

  staffFormQueryReset() {
    if (this.$refs.staffFormQueryRef) {
      this.$refs.staffFormQueryRef.resetFields()
      this.staffFormQueryModel = toStaffQO()
    }
  }

  staffFormQuerySearch() {
    this.staffFormQuerySubmitting = true
    this.staffListLoading = true
    this.staffListRowOnChange([], [])
    this.staffListSelectCurrentRowIndex = -1
    this.staffListSelectCurrentRowData = toStaffVO()
    this.staffStore.queryVo(this.$toParam<StaffQO>(this.$toSearch({ ...this.staffFormQueryModel }))).then((res: Res<StaffVO>) => {
      if (res.succ) {
        this.staffListPaginationVisible = false
        this.staffListDataSet = [ ...res.data ]
        this.staffListDataCount = res.count
        setTimeout(() => { this.staffFormQuerySubmitting = false }, 5000)
        setTimeout(() => { this.staffListLoading = false }, 500)
      } else {
        this.$message.error(res.msg, 6)
        setTimeout(() => { this.staffFormQuerySubmitting = false }, 5000)
        setTimeout(() => { this.staffListLoading = false }, 500)
      }
    }).catch(err => {
      setTimeout(() => { this.staffFormQuerySubmitting = false }, 5000)
      setTimeout(() => { this.staffListLoading = false }, 500)
    })
  }

  staffEditDrawerVisible: boolean = false

  staffFormSaveSubmitting: boolean = false

  staffFormSaveContinue: boolean = false

  staffFormModel: StaffVO = toStaffVO()

  staffFormRules: object = {
    staffNo: [
      { type: 'string', required: true, message: '工号不能为空！', trigger: ['change', 'blur'] },
    ],
    staffName: [
      { type: 'string', required: true, message: '姓名不能为空！', trigger: ['change', 'blur'] },
    ],
    // gender: [
    //   { type: 'string', required: true, message: '性别不能为空！', trigger: ['change', 'blur'] },
    // ],
    // mphoneNo: [
    //   { type: 'string', required: true, message: '手机号不能为空！', trigger: ['change', 'blur'] },
    // ],
    // idCard: [
    //   { type: 'string', required: true, message: '身份证号不能为空！', trigger: ['change', 'blur'] },
    // ],
    // emergContName: [
    //   { type: 'string', required: true, message: '紧急联系人姓名不能为空！', trigger: ['change', 'blur'] },
    // ],
    // emergContMphoneNo: [
    //   { type: 'string', required: true, message: '紧急联系人手机号不能为空！', trigger: ['change', 'blur'] },
    // ],
    // description: [
    //   { type: 'string', required: true, message: '描述不能为空！', trigger: ['change', 'blur'] },
    // ],
    // weightOrder: [
    //   { type: 'number', required: true, message: '排序权重不能为空！', trigger: ['change', 'blur'] },
    // ],
  }

  staffPostListOrgPostTreeData: Array<CoreTree<any>> = []

  staffDefaultPostIdOrgPostTreeData: Array<CoreTree<any>> = []

  staffEditFileSelectDrawerVisible: boolean = false

  staffFormFileUploadChange(value) {
    this.staffFormModel.avatarUrl = value
  }

  staffFormAvatarUrlSelectFileClick() {
    this.staffEditFileSelectDrawerVisible = true
  }

  staffFormFileSelectChange(fileUrlList) {
    this.staffFormModel.avatarUrl = fileUrlList[0]
    this.staffEditFileSelectDrawerVisible = false
  }

  staffPostListChange(value, label, extra) {
    this.staffDefaultPostIdOrgPostTreeData = []
    if (!value.includes(this.staffFormModel.defaultPostId)) {
      this.staffFormModel.defaultPostId = null
    }
    this.postStore.getOrgPostTreeData(this.$toParam<PostQO>({ postList: value } as PostQO)).then((res: Res<CoreTree<any>>) => {
      if (res.succ) {
        this.staffDefaultPostIdOrgPostTreeData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  staffFormReset() {
    if (this.$refs.staffFormRef) {
      this.$refs.staffFormRef.resetFields()
      this.staffFormModel = toStaffVO()
      this.$refs.staffFormFileUploadRef.resetFiles()
      this.staffDefaultPostIdOrgPostTreeData = []
    }
  }

  staffFormSubmit() {
    this.staffFormSaveSubmitting = true
    this.$refs.staffFormRef.validate().then(() => {
      if (this.staffFormModel.id) {
        this.staffStore.update(this.$toParam<StaffVO>(this.staffFormModel)).then((res: Res<StaffVO>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.staffFormReset()
            this.staffFormSaveSubmitting = false
            if (!this.staffFormSaveContinue) {
              this.staffEditDrawerVisible = false
              this.staffListGetDataSet()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.staffFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.staffFormSaveSubmitting = false }, 3000)
        })
      } else {
        this.staffStore.save(this.$toParam<StaffVO>(this.staffFormModel)).then((res: Res<StaffVO>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.staffFormReset()
            this.staffFormSaveSubmitting = false
            if (!this.staffFormSaveContinue) {
              this.staffEditDrawerVisible = false
              this.staffListGetDataSet()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.staffFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.staffFormSaveSubmitting = false }, 3000)
        })
      }
    }).catch(() => {
      setTimeout(() => { this.staffFormSaveSubmitting = false }, 3000)
    })
  }

  staffFormSave() {
    this.staffFormSaveContinue = false
    this.staffFormSubmit()
  }

  staffFormNextAdd() {
    this.staffFormSaveContinue = true
    this.staffFormSubmit()
  }

  staffAdd() {
    if (this.staffFormModel.id) {
      this.staffFormReset()
    }
    this.staffPostListOrgPostTreeData = []
    this.staffDefaultPostIdOrgPostTreeData = []
    this.postStore.getOrgPostTreeData(this.$toParam<PostQO>({} as PostQO)).then((res: Res<CoreTree<any>>) => {
      if (res.succ) {
        this.staffPostListOrgPostTreeData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.postStore.getOrgPostTreeData(this.$toParam<PostQO>({ postList: [] } as PostQO)).then((res: Res<CoreTree<any>>) => {
      if (res.succ) {
        this.staffDefaultPostIdOrgPostTreeData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.staffEditDrawerVisible = true
  }

  staffUpdate() {
    this.staffPostListOrgPostTreeData = []
    this.postStore.getOrgPostTreeData(this.$toParam<PostQO>({} as PostQO)).then((res: Res<CoreTree<any>>) => {
      if (res.succ) {
        this.staffPostListOrgPostTreeData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.staffStore.pkVo(this.$toParam<StaffQO>({ id: this.staffListSelectData[0].id } as StaffQO)).then((res: Res<StaffVO>) => {
      if (res.succ) {
        this.staffFormReset()
        this.staffFormModel = { ...res.data[0] }
        this.staffDefaultPostIdOrgPostTreeData = []
        this.postStore.getOrgPostTreeData(this.$toParam<PostQO>({ postList: this.staffFormModel.postList } as PostQO)).then((res: Res<CoreTree<any>>) => {
          if (res.succ) {
            this.staffDefaultPostIdOrgPostTreeData = [ ...res.data ]
          } else {
            this.$message.error(res.msg, 6)
          }
        })
        this.staffEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  staffEditDrawerOnClose() {
    if (this.staffFormSaveContinue) {
      this.staffFormSaveContinue = false
      this.staffListGetDataSet()
    }
  }

  staffDelete() {
    let param: Array<Staff> = []
    this.staffListSelectData.forEach((eo: StaffVO) => {
      param.push({ id: eo.id } as Staff)
    })
    this.staffStore.removeBatch(this.$toParam<Staff>(param)).then((res: Res<Staff>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
        this.staffListGetDataSet()
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  staffDetailDrawerVisible: boolean = false

  staffDetailModel: StaffVO = toStaffVO()

  staffDetailInfo() {
    this.staffStore.pkVo(this.$toParam<StaffQO>({ id: this.staffListSelectData[0].id } as StaffQO)).then((res: Res<StaffVO>) => {
      if (res.succ) {
        this.staffDetailModel = { ...res.data[0] }
        this.staffDetailDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  staffDetailUpdate() {
    this.staffPostListOrgPostTreeData = []
    this.postStore.getOrgPostTreeData(this.$toParam<PostQO>({} as PostQO)).then((res: Res<CoreTree<any>>) => {
      if (res.succ) {
        this.staffPostListOrgPostTreeData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.staffStore.pkVo(this.$toParam<StaffQO>({ id: this.staffDetailModel.id } as StaffQO)).then((res: Res<StaffVO>) => {
      if (res.succ) {
        this.staffFormReset()
        this.staffFormModel = { ...res.data[0] }
        this.staffDefaultPostIdOrgPostTreeData = []
        this.postStore.getOrgPostTreeData(this.$toParam<PostQO>({ postList: this.staffFormModel.postList } as PostQO)).then((res: Res<CoreTree<any>>) => {
          if (res.succ) {
            this.staffDefaultPostIdOrgPostTreeData = [ ...res.data ]
          } else {
            this.$message.error(res.msg, 6)
          }
        })
        this.staffDetailDrawerVisible = false
        this.staffEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  mounted() {
    this.staffListGetDataSet()
  }

}
</script>

<style scoped lang="less">

</style>
